// ml:run = $bin < input
// ml:ccf += -g
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <set>

int const maxn = 100007;
std::vector<int> a;
std::vector<int> id[maxn];
std::unordered_map<int, int> index;
int n;

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin >> n; a.resize(n);
    std::set<int> s;
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
        s.insert(a[i]);
    }
    int m = s.size();
    int t = 0;
    for (auto i : s) index[i] = t++;
    for (int i = 0; i < n; i++)
        id[index[a[i]]].push_back(i);

    long long ans = 0;
    long long tot = n;
    for (int i = 0; i < m; i++) {
        auto last = *id[i].rbegin();
        ans += id[i].size();
        tot -= id[i].size();
        for (int j = i + 1; j < m; j++) {
            auto tmp = *id[j].rbegin();
            for (int k = id[j].size() - 1; k >= 0; k--) {
                if (id[j][k] > last) {
                    id[j].pop_back();
                    ans++;
                    tot--;
                }
            }
            if (!id[j].empty()) break;
            last = tmp;
            i++;
        }
        ans += tot;
    }
    std::cout << ans << "\n";
}

